﻿namespace Ex04.Menus.Delegates
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    public class MenuItem : ISelector
    {
        #region Data Members
        private readonly string r_Title;
        private OperateFunctionDelegate m_delegateFunction;
        #endregion

        #region Constructor
        public MenuItem(string i_Title)
        {
            r_Title = i_Title;
        }
        #endregion

        #region Public Methods
        public void AttachActionToExecute(OperateFunctionDelegate i_ActionToExecute)
        {
            m_delegateFunction += i_ActionToExecute;
        }

        public void DetachActionToExecute(OperateFunctionDelegate i_ActionToExecute)
        {
            m_delegateFunction -= i_ActionToExecute;
        }

        public void onSelect()
        {
            Console.Clear();

            if (m_delegateFunction != null)
            {
                ExecuteAction();
                Menu.pressAnyKey();
            }
        }

        public override string ToString()
        {
            return r_Title;
        }
        #endregion

        #region Private Methods
        private void ExecuteAction()
        {
            if (m_delegateFunction != null)
            {
                m_delegateFunction.Invoke();
            }
        }

        #endregion
    }
}
